package com.example.demo.routes;


import com.example.demo.filter.CacheRequestBodyFunction;
import com.example.demo.filter.DeregisterInstanceGatewayFilter;
import com.example.demo.filter.FallbackGatewayFilter;
import com.example.demo.filter.GatewayMetricsGatewayFilter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GatewayRouteConfig {

  @Bean
  public RouteLocator routes(RouteLocatorBuilder builder,
      DeregisterInstanceGatewayFilter deregisterInstanceGatewayFilter, FallbackGatewayFilter fallbackGatewayFilter,
      GatewayMetricsGatewayFilter gatewayMetricsGatewayFilter) {
    return builder.routes()
        .route("http", r ->
            r.path("/execute")
                .filters(f -> f.modifyRequestBody(String.class, String.class, new CacheRequestBodyFunction())
                    .filter(fallbackGatewayFilter)
                    .filter(gatewayMetricsGatewayFilter)
                )
                .uri("http://localhost:18080")
        )
        .route("DeregisterInstance", r -> r.path("/discovery/deregister")
            .filters(f -> f.filter(deregisterInstanceGatewayFilter))
            .uri("https://example.com"))
        .build();
  }
}
